package www.sofn.com.address;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import www.sofn.com.address.domain.pojo.AddressPO;
import www.sofn.com.address.domain.vo.AddressVO;
import www.sofn.com.address.domain.vo.DefaultAddressVO;
import www.sofn.com.domain.R;
import www.sofn.com.validate.group.InsertGroup;
import www.sofn.com.validate.group.UpdateGroup;

import java.util.List;


@RestController
@Tag(name = "地址")
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;

    @Operation(summary = "获取会员地址列表")
    @GetMapping()
    public R<List<AddressVO>> list(){
      return  R.ok( addressService.findAll());
    }


    @Operation(summary = "获取收货地址详情")
    @GetMapping("/{id}")
    public R<AddressVO> getById(@PathVariable(value = "id", required = true) String id) {
        return R.ok(addressService.getById(id));

    }

    @Operation(summary = "获取默认的收货地址")
    @GetMapping("/default_address")
    public R<DefaultAddressVO> getDefaultAddress() {
        return R.ok(addressService.getDefaultAddress());

    }


    @Operation(summary = "新增收货地址")
    @PostMapping
    public R insert(@RequestBody @Validated(value = {InsertGroup.class}) AddressVO addressVO) {
        addressService.insert(BeanUtil.toBean(addressVO, AddressPO.class));
        return R.ok();
    }

    @Operation(summary = "编辑收货地址")
    @PutMapping
    public R update(@RequestBody @Validated(value = {UpdateGroup.class}) AddressVO addressVO) {
        addressService.updateById(BeanUtil.toBean(addressVO, AddressPO.class));
        return R.ok();
    }

    @Operation(summary = "删除收货地址")
    @DeleteMapping("/{id}")
    public R delById(@PathVariable(value = "id", required = true) String id) {
        addressService.delById(id);
        return R.ok();
    }


    @Operation(summary = "设置默认状态")
    @GetMapping("/set_default_status/{id}")
    public R setDefaultStatus(@PathVariable(value = "id",required = true ) String id){
        addressService.changeDefaultStatus(id);
        return R.ok();
    }




}
